package com.hawk.eye.server.controller.hawkeye;


import com.hawk.eye.common.core.ApiResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.convert.WhiteListConvert;
import com.hawk.eye.server.domain.dto.resp.WhiteListRespDTO;
import com.hawk.eye.server.domain.vo.req.WhiteListReqVO;
import com.hawk.eye.server.domain.vo.resp.WhiteListRespVO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hawk.eye.server.service.IWhiteListService;

/**
 * 白名单 Controller
 *
 * @author salad
 * @date 2023-10-04
 */
@RestController
@RequestMapping("/server/whitelist")
@RequiredArgsConstructor
public class WhiteListController {

    private final IWhiteListService whiteListService;

    /**
     * 分页查询白名单
     * @param pageQuery
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:whitelist:query')")
    @GetMapping("/page")
    public PageResult<WhiteListRespVO> page(PageQuery pageQuery, WhiteListReqVO reqVO) {
        PageResult<WhiteListRespDTO> pageResult = whiteListService.page(pageQuery, WhiteListConvert.INSTANCE.to(reqVO));
        return PageResult.of(pageResult, WhiteListConvert.INSTANCE::to);
    }

    /**
     * 保存白名单
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:whitelist:add')")
    @PostMapping("/save")
    public ApiResult save(@RequestBody @Validated WhiteListReqVO reqVO) {
        boolean status = whiteListService.save(WhiteListConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 修改白名单
     * @param reqVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:whitelist:edit')")
    @PutMapping("/update")
    public ApiResult update(@RequestBody @Validated WhiteListReqVO reqVO) {
        boolean status = whiteListService.update(WhiteListConvert.INSTANCE.to(reqVO));
        return ApiResult.status(status);
    }

    /**
     * 白名单详情
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:whitelist:query')")
    @GetMapping(value = "detail/{id}")
    public ApiResult<WhiteListRespVO> detail(@PathVariable("id") Long id) {
        return ApiResult.ok(WhiteListConvert.INSTANCE.to(whiteListService.detail(id)));
    }

    /**
     * 删除白名单
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('hawk-eye:whitelist:remove')")
    @DeleteMapping("/remove/{ids}")
    public ApiResult remove(@PathVariable Long[] ids) {
        return ApiResult.status(whiteListService.removeBatchByIds(CollectionUtil.toList(ids)));
    }

}
